package com.rulin.asp.business.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.rulin.asp.business.entity.AppointParam;
import com.rulin.asp.business.entity.Question;
import com.rulin.asp.business.entity.QuestionAdminParam;
import com.rulin.asp.business.entity.QuestionAdminResultRow;
import com.rulin.asp.business.entity.QuestionParam;
import com.rulin.asp.business.log.entity.QuestionLog;
import com.rulin.asp.business.log.service.QuestionLogService;
import com.rulin.asp.business.service.DictionaryService;
import com.rulin.asp.business.service.QuestionService;
import com.rulin.asp.sso.entity.UserUtils;
import com.rulin.asp.sso.service.UserService;
import com.rulin.asp.util.Consists;
import com.rulin.asp.util.Tools;

@Controller
@RequestMapping("/adminQuesList")
public class AdminQuesListController {

	@Autowired
	private QuestionService questionService;
	
	@Autowired
	private DictionaryService dictionaryService;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private QuestionLogService questionLogService;
	
	@RequestMapping(value = "/quesListPage.html", method = RequestMethod.GET)
	public String quesListPage(Model model){
		setDiv(model);
		model.addAttribute("users", userService.getAllTeachers());
		return "business/adminqueslist";
	}
	
	public void setDiv(Model model){
		model.addAttribute("quesTypes", dictionaryService.getByModel("ques_type_param"));
		model.addAttribute("quesDiffLevels", dictionaryService.getByModel("ques_diff_level"));
		
		model.addAttribute("questionSources", dictionaryService.getByModel("question_source"));
		model.addAttribute("quesBookClasses", dictionaryService.getByModel("ques_book_class"));
		model.addAttribute("quesBookClasses1", dictionaryService.getByModel("ques_book_class_1"));
		model.addAttribute("quesBookClasses2", dictionaryService.getByModel("ques_book_class_2"));
		model.addAttribute("quesBookClasses3", dictionaryService.getByModel("ques_book_class_3"));
		model.addAttribute("userTypes", dictionaryService.getByModel("user_type"));
		model.addAttribute("quesBookClasses4", dictionaryService.getByModel("ques_book_class_4"));
	}
	
	@RequestMapping(value = "/getList.json", method = RequestMethod.GET)
	@ResponseBody
	public String list(QuestionParam bean){
		Pair<Integer,List<QuestionAdminResultRow>> pair= questionService.getAdminQuesListByParam(bean);
		return Tools.toEasyuiDataGrid(pair.getLeft(), pair.getRight());
	}
	
	/**
	 * 指派给教师
	 * @param param
	 */
	@RequestMapping(value = "/appoint.json", method = RequestMethod.GET)
	public void appoint(AppointParam param){
		Question question = new Question();
		question.setQuesId(param.getQuesId());
		question.setClaimUserAccount(param.getUserAccount());
		question.setClaimUserName(param.getUserName());
		question.setClaimTime(new Date());
		question.setQuesType(Consists.QUES_TYPE_3);
		questionService.saveQuestion(question);
		
		//保存题目日志信息
		QuestionLog log = new QuestionLog();
		log.setAcceptorAccount(param.getUserAccount());
		log.setAcceptorName(param.getUserName());
		log.setQuesId(param.getQuesId());
		log.setOperateType("8");
		log.setUserAccount(UserUtils.getUser().getAccount());
		log.setUserName(UserUtils.getUser().getUserName());
		questionLogService.addQuestionLog(log);
	}
	
	@RequestMapping(value = "/openEditPage.html", method = RequestMethod.GET)
	public String openEditPage(Model model,String quesId){
		setDiv(model);
		model.addAttribute("form", questionService.getQuestion(quesId));
		return "business/quescollection";
	}
	
	@RequestMapping(value = "/openReadPage.html", method = RequestMethod.GET)
	public String openReadPage(Model model,String quesId){
		setDiv(model);
		Question question = questionService.getQuestion(quesId);
		model.addAttribute("form", question);
		if("4".equals(question.getQuesType()) && "4".equals(question.getQuestionSource())){
			model.addAttribute("quesId", quesId);
			return "redirect:/errorQuesList/openApprovalPage.html";
		}
		if("5".equals(question.getQuesType()) && "4".equals(question.getQuestionSource())){
			model.addAttribute("quesId", quesId);
			return "redirect:/errorQuesList/openCompletePage.html";
		}
		if("4".equals(question.getQuestionSource())){
			model.addAttribute("quesId", quesId);
			return "redirect:/errorQuesList/openReadPage.html";
		}
		return "business/quesadminread";
	}
	
	/**
	 * 注销题目
	 * @param ids
	 */
	@RequestMapping(value = "/deletes.json", method = RequestMethod.POST)
	public void deletes(QuestionAdminParam param){
		questionService.deleteQuestionByQuesIds(param.getIds());
		
		//保存日志信息
		List<String> ids = param.getIds();
		for (String id : ids) {
			QuestionLog log = new QuestionLog();
			log.setQuesId(id);
			log.setOperateType("9");
			log.setUserAccount(UserUtils.getUser().getAccount());
			log.setUserName(UserUtils.getUser().getUserName());
			questionLogService.addQuestionLog(log);
		}
	}
	
	/**
	 * 发布题目
	 * @param ids
	 */
	@RequestMapping(value = "/publish.json", method = RequestMethod.POST)
	public void publish(QuestionAdminParam param){
		questionService.publishQuesByQuesIds(param.getIds());
		
		//保存日志信息
		List<String> ids = param.getIds();
		for (String id : ids) {
			QuestionLog log = new QuestionLog();
			log.setQuesId(id);
			log.setOperateType("10");
			log.setUserAccount(UserUtils.getUser().getAccount());
			log.setUserName(UserUtils.getUser().getUserName());
			questionLogService.addQuestionLog(log);
		}
	}
	
	/**
	 * 导出并下载今日统计数据
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value="/download", method=RequestMethod.GET)
	public void download(HttpServletResponse response) throws IOException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String fileName = sdf.format(new Date())+".xls";
		
		response.setContentType("multipart/form-data");
		response.setHeader("Content-Disposition", "attachment;filename=" + fileName);

		OutputStream os = response.getOutputStream();
		questionService.getExportTodayQuestionStatistics().export(os);
	}
	
	/**
	 * 导入excel上传的题目
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	public void importExcel(HttpServletRequest request,HttpServletResponse response) throws Exception{
		MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request;
		for (Map.Entry<String, MultipartFile> entry :  mRequest.getFileMap().entrySet()) {
			MultipartFile file = entry.getValue();
			questionService.importQuestions(file.getInputStream(), file.getOriginalFilename());
		}
	}
}
